//
//  UserCourseViewController.m
//  ClassRoom
//
//  Created by apple on 12-12-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserCourseViewController.h"
#import "UserCourseTableViewCell.h"
#import "CourseInformationViewController.h"
#import "JSONKit.h"
#import "ASIHTTPRequest.h"
#import "UrlData.h"
#import "EvaluateViewController.h"

@interface UserCourseViewController ()

@end

@implementation UserCourseViewController
@synthesize usercourseTableView;
@synthesize usercoursetitleArray;
@synthesize resultDic;
@synthesize waitIndicatorView;
@synthesize loadinglabel;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"我的课程";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
  
    // Do any additional setup after loading the view from its nib.
    usercoursetitleArray=[[NSMutableArray alloc]init];
    //初始化服务器返回的结果字典
    resultDic=[[NSMutableDictionary alloc]init];
    
    //向服务器请求数据
    [self requestData];
    [self displayActiveIndicatorView];//显示等待ActiveIndicatorView
  
    
    //*****************
    
    //测试评价界面
    /*
    NSInteger index = 1;
    EvaluateViewController *evaluateViewcontroller=[[EvaluateViewController alloc]init];
    
    evaluateViewcontroller.courseInfoDic=[[NSMutableDictionary alloc]init];
    evaluateViewcontroller.courseInfoDic=[[usercoursetitleArray objectAtIndex:index] objectForKey:@"course"];
    [self.navigationController pushViewController:evaluateViewcontroller animated:YES];
    
   */
    //********
     //测试结束
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark tableviewsell里button点击
- (void)detailButtonPressed:(id)sender
{
    NSInteger index = ((UIButton *)sender).tag;
    NSLog(@"!!!!index=%d",index);
    //object = array(index);
    CourseInformationViewController* courseInfoViewcontroller=[[CourseInformationViewController alloc]init];
    [self.navigationController pushViewController:courseInfoViewcontroller animated:YES];
    [courseInfoViewcontroller SetSomeChanges:(NSDictionary *)[[[usercoursetitleArray objectAtIndex:index] objectForKey:@"course"]objectForKey:@"information"]buttonEnable:NO];
}

- (void)evaluateButtonPressed:(id)sender
{
    NSInteger index = ((UIButton *)sender).tag;
    EvaluateViewController *evaluateViewcontroller=[[[EvaluateViewController alloc]init] autorelease];
    
    evaluateViewcontroller.courseInfoDic=[[NSMutableDictionary alloc]init];
    evaluateViewcontroller.courseInfoDic=[[usercoursetitleArray objectAtIndex:index] objectForKey:@"course"];
    [self.navigationController pushViewController:evaluateViewcontroller animated:YES];
    
}

#pragma mark -
#pragma mark ASIHTTPRequest
-(void)requestData
{
    NSString *urlString=[self geturlString];//生成url
    
    NSLog(@"urlstring=%@",urlString);
    NSURL *url=[NSURL URLWithString:urlString];
    NSLog(@"url=%@",url);
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request setDelegate:self];
    [request startAsynchronous];
    
}

//生成url
-(NSString *)geturlString
{
    NSString* urlString=[NSString stringWithFormat:@"%@%@sum=20&page=0&userID=10001",ClassRoom_URL,UserCourse_URL]; 
  
    NSLog(@"urlString=%@",urlString);
    return urlString;
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    NSLog(@"requestFinished");
    NSString *responseString = [request responseString];
    NSLog(@"responseString------>>%@",responseString);
    
    resultDic=[[responseString objectFromJSONString] retain];//将所有的结果放到其中,接下来根据key取出相应的数据
    NSLog(@"resultDic--->>>%@",resultDic);
    //if (searchCondition==-1) {
 
        
    [self setCourseArray];
    [usercourseTableView reloadData];
        
        
    [self dismissActiveIndicatorView];//结束显示ActiveIndicatorView
    
    
    
}

-(void)setCourseArray// 设置课程列表，array中的值为字典
{   
     NSMutableDictionary* courseDic=[resultDic objectForKey:@"userCourses"];
    NSLog(@"courseDic   aaaaa=%@",courseDic);
    //usercoursetitleArray=[resultDic objectForKey:@"userCourses"];
    for (id key in courseDic) {
        [usercoursetitleArray addObject:[courseDic objectForKey:key]];
    }
    [usercoursetitleArray retain];
    NSLog(@"courseResultArray=%@",usercoursetitleArray);
}   

#pragma mark -
#pragma mark UIActivityIndicatorView

//开始显示 UIActivityIndicatorView
- (void) displayActiveIndicatorView 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    if (waitIndicatorView == nil) {
        waitIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 100, 46, 46)];
        loadinglabel=[[UILabel alloc]initWithFrame:CGRectMake(140, 140, 80, 30)];
        loadinglabel.text=@"loading...";
        loadinglabel.BackgroundColor=[UIColor clearColor];
        [waitIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        [waitIndicatorView startAnimating];
        
        [self.view addSubview:waitIndicatorView];
        [self.view addSubview:loadinglabel];
    }
    
    [pool release];
}
//结束显示 UIActivityIndicatorView
- (void) dismissActiveIndicatorView
{
    if (waitIndicatorView)
    {
        [waitIndicatorView stopAnimating];
        [waitIndicatorView removeFromSuperview];
        [waitIndicatorView release]; waitIndicatorView = nil;
        [loadinglabel removeFromSuperview];
        [loadinglabel release];
        loadinglabel=nil;
    }
}


#pragma mark -
#pragma mark UITableView

- (void)setTableView{
    //    courseRecommendTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain] autorelease];
    //    courseRecommendTableView.delegate = self;
    //    courseRecommendTableView.dataSource = self;
    //    [self.view addSubview:courseRecommendTableView];
    [usercourseTableView reloadData];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [usercoursetitleArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (480-22-44)/4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UserCourseTableViewCell *cell = (UserCourseTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    
    if (cell == nil) {
        cell = [[[UserCourseTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.tag = indexPath.row;
    cell.detailButton.tag=indexPath.row;
    cell.evaluateButton.tag=indexPath.row;
    [cell.detailButton addTarget:self action:@selector(detailButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [cell.evaluateButton addTarget:self action:@selector(evaluateButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    //设置 title
    if (usercoursetitleArray.count >0) {
        NSMutableDictionary *courseDic=[[usercoursetitleArray objectAtIndex:indexPath.row] objectForKey:@"course"];
           
        NSMutableDictionary *infoDic=[courseDic objectForKey:@"information"];
        NSString *courseNameStr=[infoDic objectForKey:@"courseName"];
        [cell setCellData:courseNameStr];
         NSLog(@"vvvvvcourse=%@",courseDic);
    }
    return cell;
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



@end
